﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sispe.Entities;
using Sispe.BussinesLogic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace Sispe.Mvc4.Controllers
{
    public class CondicionController : Controller
    {
        [Dependency]
        public ICondicionesBL condicionBL { get; set; }

        [Dependency]
        public IContratoBL contratoBL { get; set; }

        public ActionResult Index(int id = 0)
        {
            if (id == 0)
            {
                return RedirectToAction("Index", "Contrato");
            }
            else
            {
                var condiciones  = (from c in condicionBL.GetAllFromCondiciones()
                                                      where c.IdContrato.Equals(id)
                                                      select c).ToList();

                var contrato = contratoBL.GetFromContratoById(id);
                if (contrato == null)
                {
                    return RedirectToAction("Index", "Contrato");
                }

                ViewData["IdContrato"] = contrato.IdContrato;
                ViewData["TRABAJADOR"] = contrato.TRABAJADOR.PERSONA.NombreCompleto;

                return View(condiciones);
            }
        }

        public ActionResult Create(int id = 0)
        {
            ViewData["IdContrato"] = id;
            return View();
        }

        [HttpPost]
        public ActionResult Create(FormCollection colection)
        {
            try
            {
                CONDICIONE condicion = new CONDICIONE()
                {
                    IdContrato = Int32.Parse(colection["IdContrato"].ToString()),
                    IdTipoCondicion = 1,
                    Imprimir = true,
                    Observacion = colection["Condicion"].ToString(),
                };

                condicionBL.AddCondiciones(condicion);
                return RedirectToAction("Index", "Condicion", new { @id = condicion.IdContrato });
            }
            catch (Exception ex)
            {
                TempData["AlertMessage"] = ex.Message;
                return RedirectToAction("Create");
            }
        }


        public ActionResult Edit(int id = 0)
        {

            if (id == 0)
            {
                return View();
            }

             var currCondicion = new CONDICIONE();

             var condicion = (from c in condicionBL.GetAllFromCondiciones()
                                                  where c.IdCondicion.Equals(id)
                                                  select c).ToList();
             foreach (var item in condicion.Where(c => c.IdCondicion.Equals(id)))
            {
                currCondicion.IdTipoCondicion = 1;
                currCondicion.Imprimir = true;
                currCondicion.Observacion = item.Observacion;
                currCondicion.IdContrato = item.IdContrato;
                currCondicion.IdCondicion = item.IdCondicion;
            }

            if (currCondicion == null)
            {
                return RedirectToAction("Index", "Contrato");
            }

            return View(currCondicion);
        }


        [HttpPost]
        public ActionResult Edit(int id, FormCollection colection)
        {
            try
            {
                CONDICIONE condicion = new CONDICIONE()
                {
                    IdCondicion = Int32.Parse(colection["IdCondicion"].ToString()),
                    IdContrato = Int32.Parse(colection["IdContrato"].ToString()),
                    Observacion = colection["Condicion"].ToString(),
                };

                condicionBL.UpdateCondiciones(condicion);
                return RedirectToAction("Index", "Condicion", new { @id = condicion.IdContrato });
            }
            catch (Exception ex)
            {
                TempData["AlertMessage"] = ex.Message;
                return View();
            }
        }


    }
}
